#include <SoftwareSerial.h>
SoftwareSerial BT(0,1);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <math.h>
#include <MsTimer2.h>


int BH1750address = 0x23;//BH1750 I2C地址
byte buff[2];

int i=0;//光
int flag = 0;//定时中断标志
char key='H',key1;
int flag1=-1;

int t=0;//adjust
int redhot=2;//数字引脚但可以插入模拟输入口
int MQ135=A3;//模拟输入端，有毒气体
int bee=A4;//模拟输入端，蜂鸣器
int LED=A5;
//时钟定义
int hours=0,minutes=0,seconds=0;


void timer()//定时中断函数
{
  flag = 1;
}


tmElements_t tm;

void setup() {
Serial.begin(9600);
Wire.begin();//光感模块的初始化
MsTimer2::set(2000, timer); //定时器设置,每2秒触发一次timer函数操作
MsTimer2::start();
pinMode(LED,OUTPUT);

Serial.println("BT is ready");
pinMode(redhot,OUTPUT);
pinMode(MQ135,INPUT);
pinMode(bee,OUTPUT);
}

//时钟函数
void print2digits(int num)
{
  if(num>=0&&num<10)
  Serial.write('0');
  Serial.print(num);
}

void printtime()
{
  if(RTC.read(tm)){
    Serial.print("time=");
    print2digits(tm.Hour);
    Serial.write(':');
    Serial.print(tm.Minute);
    Serial.write(':');
    Serial.println(tm.Second);
    Serial.print(",DATA=");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.println(tmYearToCalendar(tm.Year));
  }
  else{
    if(RTC.chipPresent())
    {Serial.println("The DS1037 isstopped.Please run the SetTime");
    Serial.println("example to initialize the timt and begin running.");
    }else{
      Serial.println("DS1307 read error! Please check the circuitry");
    }
    delay(9000);
  }
  delay(1000);
}

//手动的灯光强弱控制
void adjustLED()
{
  char val;
  while(Serial.available())
 {val=Serial.read();
  if(val=='1')
  {
    analogWrite(LED,t);
    t=t+2;
  }
  if(val=='0')
   {
    analogWrite(LED,t);
    t=t-2; 
   }
 }
}
//红外热释开关
void hotpeople()
{
  int s,i;
  s=digitalRead(redhot);
  if(s==HIGH)
  { hotpeople();
    delay(1000);
    if(s==HIGH)
    {
     for(i=0;i<=255;i++)
       {
        analogWrite(LED,i);
        delay(10);
        }
    }
  }
  else
   digitalWrite(LED,LOW);
}

//有毒气体检测报警
void airpoison()
{
  float voltage;
  voltage = getVoltage(MQ135);
  if(voltage>=1.0)
  {
     delay(1000);
     if(voltage>=1.0);
    for(i=0;i<=255;i+=5)
    { 
      analogWrite(bee,HIGH);
      analogWrite(LED,i);
    }
  }
}

float getVoltage(int pin){
  
  return (analogRead(pin) * 0.004882814);
}


//闹钟调整
void clockeron(int h,int m,int s)
{
      key=Serial.read();
      key1=Serial.read();
       if(key=='H'&&key1=='1')
        {
          hours++;
          if(hours==24)
          hours=0;
          }
       else if(key=='H'&&key1=='0')
         {
          hours--;
          if(hours==0)
          hours=24;
          } 
        else if(key=='M'&&key1=='1')
            {
              minutes++;
              if(minutes==60)
              minutes=0;
          }
       else if(key=='M'&&key1=='0')
        {
          minutes--;
          if(minutes==0)
          minutes=60;
          } 

       else if(key=='S'&&key1=='1')
            {
              seconds++;
              if(seconds==60)
              seconds=0;
          }
       else if(key=='S'&&key1=='0')
        {
          seconds--;
          if(seconds==0)
          seconds=60;
          } 
 
  BT.print("your clock is:");
  BT.println(hours);
  BT.print(":");
  BT.print(minutes);
  BT.print(":");
  BT.println(seconds);
}

void clocks(int h,int m,int s){
 if(h==tm.Hour&&m==tm.Minute&&s==tm.Second)//drawn
 for(int i=0;i<=255;i++)
  {
    analogWrite(LED,i);
    delay(15);
  } 
}

//光感模块

double BH1750() //BH1750设备操作
{
  int i=0;
  double  val=0;
  //开始I2C读写操作
  Wire.beginTransmission(BH1750address);
  Wire.write(0x10);//1lx reolution 120ms//发送命令
  Wire.endTransmission();  
  
  delay(200);
  //读取数据
  Wire.beginTransmission(BH1750address);
  Wire.requestFrom(BH1750address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  if(2==i)
  {
   val=((buff[0]<<8)|buff[1])/1.2;
  }
  return val;
}

void GY30()
{
  Serial.println("asfdj");
  double x;
  x=BH1750();
  if(  flag )//
  {
    if(x<4000)
     {
      while(x<500)
      {Serial.println("turn on");
      analogWrite(2,255);//turn on LED change the sunshine,
      x=BH1750();
      }
     }
     
    if(x>800)
    {
      analogWrite(2,0);
      
    }
    if(x>2200)
    {
      analogWrite(LED,0);//turn off LED ,turn on moter adjust the curtain用步进电机
    }
    Serial.println("光强：");
    Serial.print( BH1750() );
    Serial.println("[lux]");
    flag = 0;//归零，等着定时中断重新赋值
  }
}


//主函数
void loop() {
char ch,t='0',p='0';
char q='0';
  q=Serial.read();
  Serial.println(q);
while(Serial.available()&&q=='1')
   {  ch=Serial.read();
   Serial.println(ch);
     if(ch=='A'){
      flag1=1;
   }
    else if(ch=='N')
     {flag1=0;}
   }
if(flag1==1){
  while(Serial.available()&&q=='2')
  {
    key=Serial.read();
    BT.println("please adjust your clock!(please input from a to x,<eg:1Ha 1Mb or 1Sc");
    clockeron(tm.Hour,tm.Minute,tm.Second);
  }
 clocks(hours,minutes,seconds);
 hotpeople();
}
else if(flag1==0){
  Serial.println("ads");
  GY30();
  while(Serial.available()&&q=='3'){
   char  val=Serial.read();
    while(val!='0')
    { 
       if(val=='2')
        t='x';
       else if(val=='3')
        p='m'; 
        val=Serial.read();
   }
  if(t=='x')
   GY30();
  if(p=='m')
  hotpeople();
  }
  while(q=='4'&&Serial.available())
    adjustLED();
 }
 
 Serial.print("now:");
 Serial.println(flag1);
 Serial.println(t);
airpoison();
}
